package com.opesoft.app.system.biz;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.opesoft.app.system.model.Dictionary;
import com.opesoft.fw.core.exception.MessageException;
import com.opesoft.pf.jpa.SessionManager;
import com.opesoft.pf.jpa.SimpleBO;
import com.opesoft.pf.jpa.SimpleQL;

public class DictionaryBO extends SimpleBO<Dictionary>
{
	Logger log=Logger.getLogger(this.getClass());

	public List allDictionaryConfig() throws Exception
	{
		EntityManager em=SessionManager.open();
		try
		{
			return em.createQuery("select x.id,x.code,x.name from DictionaryConfig x order by x.code").getResultList();
		}
		catch(Exception e)
		{
			throw e;
		}
		finally
		{
			SessionManager.rollback();
		}
	}

	public void insert(Dictionary data) throws Exception
	{
		EntityManager em=SessionManager.open();
		em.persist(data);
		data.setRank(data.getId());
		SessionManager.commit();
	}

	@SuppressWarnings("unchecked")
	public void moveUp(long configId,String[] ids) throws MessageException
	{
		SimpleQL ql=new SimpleQL("Dictionary");
		ql.addNumInCondition("id",ids);
		ql.setOrderBy(new String[]{"rank","id"});
		ql.setFields(new String[]{"id"});

		EntityManager em=SessionManager.open();
		List<Long> idList=em.createQuery(ql.queryQL()).getResultList();
		SessionManager.commit();

		if(idList!=null&&idList.size()>0)
		{
			for(int i=0;i<idList.size();i++)
			{
				moveUp(configId,idList.get(i));
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void moveDown(long configId,String[] ids) throws MessageException
	{
		EntityManager em=SessionManager.open();
		SimpleQL ql=new SimpleQL("Dictionary");
		ql.addNumInCondition("id",ids);
		ql.setOrderBy(new String[]{"rank","id"});
		ql.setFields(new String[]{"id"});

		List<Long> idList=em.createQuery(ql.queryQL()).getResultList();
		if(idList!=null&&idList.size()>0)
		{
			for(int i=idList.size()-1;i>=0;i--)
			{
				moveDown(configId,idList.get(i));
			}
		}

		SessionManager.commit();
	}

	@SuppressWarnings("unchecked")
	public void moveUp(long configId,long id) throws MessageException
	{
		EntityManager em=SessionManager.open();
		Dictionary dict=em.find(Dictionary.class,id);
		if(dict!=null)
		{
			String sql="select x from Dictionary x where x.rank < "+dict.getRank()+" and x.dictionaryConfigId = "+configId+" order by x.rank desc, x.id desc";
			Query q=em.createQuery(sql);
			q.setFirstResult(0);
			q.setMaxResults(1);
			List<Dictionary> list=q.getResultList();
			if(list!=null&&list.size()>0)
			{
				Dictionary dict2=list.get(0);
				log.debug("Dict2 : "+dict2.getId());
				long temp1=dict.getRank();
				long temp2=dict2.getRank();
				dict2.setRank(-1);
				em.flush();
				dict.setRank(temp2);
				em.flush();
				dict2.setRank(temp1);
				em.flush();
				SessionManager.commit();
			}
			else throw new MessageException("Can't move up dictionary! (name : "+dict.getName()+")");
		}
		else throw new MessageException("Don't find dictionary! (id : "+id+")");
	}

	@SuppressWarnings("unchecked")
	public void moveDown(long configId,long id) throws MessageException
	{
		EntityManager em=SessionManager.open();
		Dictionary dict=em.find(Dictionary.class,id);
		if(dict!=null)
		{
			String sql="select x from Dictionary x where x.rank > "+dict.getRank()+" and x.dictionaryConfigId = "+configId+" order by x.rank, x.id";
			Query q=em.createQuery(sql);
			q.setFirstResult(0);
			q.setMaxResults(1);
			List<Dictionary> list=q.getResultList();
			if(list!=null&&list.size()>0)
			{
				Dictionary dict2=list.get(0);
				log.debug("Dict2 : "+dict2.getId());
				long temp1=dict.getRank();
				long temp2=dict2.getRank();
				dict2.setRank(-1);
				em.flush();
				dict.setRank(temp2);
				em.flush();
				dict2.setRank(temp1);
				em.flush();
				SessionManager.commit();
			}
			else throw new MessageException("Can't move down dictionary! (name :"+dict.getName()+")");
		}
		else throw new MessageException("Don't find dictionary! (id : "+id+")");
	}

}
